package com.example.wc.logindemolistview;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by hui on 2017/2/6.
 */

public class HomepageFragment extends Fragment {

    private ImageButton mWriteButton;
    private ListView mBlogList;
    private RadioButton mblog;

    private Content contentSave;
    private JsonObjectRequest jsonObjectRequest;

//    ArrayAdapter重写getView()方法实现1/2
    private ArrayAdapter<Blogs> arr_aAdapter;

    //SimpleAdapter实现1/3
//    private SimpleAdapter sim_aAdapter;
//    private List<Map<String, Object>> dataList;

    //BaseAdapter自定义适配器实现
    private BlogsBaseAdapter mBlogsBaseAdapter;

    public static class Content{
        private List<Blogs> content;

        public List<Blogs> getContent() {
            return content;
        }

        public void setContent(List<Blogs> content) {
            this.content = content;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment_homepage, container, false);

        mWriteButton = (ImageButton)v.findViewById(R.id.write_button);
        mWriteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(HomepageFragment.this.getContext(), WriteActivity.class);
                startActivity(i);
            }
        });

        mBlogList = (ListView)v.findViewById(R.id.blog_list);

        jsonObjectRequest = new JsonObjectRequest("http://www.diyerland.com/diyerland/api/blogs", null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        Gson gson = new Gson();
                        contentSave = gson.fromJson(response.toString(), Content.class);
                        Log.d("TAG", contentSave.content.get(0).getName());

                        //ArrayAdapter重写getView()方法实现2/2
                                /*arr_aAdapter = new ArrayAdapter<Blogs>(HomepageFragment.this.getContext(), R.layout.blogslist_item, contentSave.content){
                                    public View getView(int position, View convertView, ViewGroup parent){

                                        Blogs blogs = getItem(position);
                                        LayoutInflater inflater = getLayoutInflater(Bundle.EMPTY);
                                        convertView = inflater.inflate(R.layout.blogslist_item, null);

                                        TextView name = (TextView) convertView.findViewById(R.id.blogs_name);
                                        TextView content = (TextView) convertView.findViewById(R.id.blogs_content);

                                        name.setText(blogs.getName());
                                        content.setText(blogs.getContent());

                                        return convertView;
                                    }
                                };
                                mBlogList.setAdapter(arr_aAdapter);*/

                        //SimpleAdapter实现2/3
                                /*dataList = new ArrayList<Map<String, Object>>();
                                sim_aAdapter = new SimpleAdapter(HomepageFragment.this.getContext(),getData(), android.R.layout.simple_list_item_2,
                                        new String[]{"name", "content"}, new int[]{android.R.id.text1, android.R.id.text2});
                                mBlogList.setAdapter(sim_aAdapter);*/

                        //BaseAdapter自定义适配器实现
//                                mBlogList.setAdapter(new BlogsBaseAdapter(HomepageFragment.this.getContext(), listTest));
                        mBlogsBaseAdapter = new BlogsBaseAdapter(HomepageFragment.this.getContext(), contentSave.content);
                        mBlogsBaseAdapter.refresh(contentSave.content);//自定义方法刷新数据
//                                mBlogsBaseAdapter.notifyDataSetChanged();//刷新可见试图列表
                        mBlogList.setAdapter(mBlogsBaseAdapter);
//
                        String jsonString = "{\"name\":\"怪盗kidou\",\"content\":24}";
                        Blogs blogs = gson.fromJson(jsonString, Blogs.class);
                        Log.d("TAG", blogs.getName());
                        Log.d("TAG", response.toString());
                    }
                    //SimpleAdapter实现3/3
                            /*private List<Map<String, Object>> getData(){
                                for (int i = 0; i < contentSave.content.size(); i++){
                                    Map<String, Object>map = new HashMap<String, Object>();
                                    map.put("name", contentSave.content.get(i).getName());
                                    map.put("content", contentSave.content.get(i).getContent());
                                    dataList.add(map);
                                }
                                return dataList;
                            }*/
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        Volley.newRequestQueue(HomepageFragment.this.getContext()).add(jsonObjectRequest);

       /* final List<Blogs> listTest = new ArrayList<Blogs>();
        for (int i = 0; i < 20; i++){
            listTest.add(new Blogs("标题" + i, "内容" + i));
        }

        mBlogsBaseAdapter = new BlogsBaseAdapter(HomepageFragment.this.getContext(), listTest);
        mBlogsBaseAdapter.refresh(listTest);//自定义方法刷新数据
//          blogsBaseAdapter.notifyDataSetChanged();//刷新可见试图列表
        mBlogList.setAdapter(mBlogsBaseAdapter);*/

        mBlogList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //position代表单击第几个选项
                Blogs blogs = contentSave.content.get(position);
                Log.d("TAG", blogs.getName());
                Toast.makeText(HomepageFragment.this.getContext(), blogs.getName(), Toast.LENGTH_SHORT).show();
            }
        });


        mblog = (RadioButton)v.findViewById(R.id.blog_radiobutton);
        mblog.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });
        mblog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Volley.newRequestQueue(HomepageFragment.this.getContext()).add(jsonObjectRequest);
//                mBlogList.setAdapter(new BlogsBaseAdapter(HomepageFragment.this.getContext(), listTest));

                /*JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://www.diyerland.com/diyerland/api/blogs", null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {

                                Gson gson = new Gson();
                                contentSave = gson.fromJson(response.toString(), Content.class);
                                Log.d("TAG", contentSave.content.get(0).getName());

                                //ArrayAdapter重写getView()方法实现2/2
                                *//*arr_aAdapter = new ArrayAdapter<Blogs>(HomepageFragment.this.getContext(), R.layout.blogslist_item, contentSave.content){
                                    public View getView(int position, View convertView, ViewGroup parent){

                                        Blogs blogs = getItem(position);
                                        LayoutInflater inflater = getLayoutInflater(Bundle.EMPTY);
                                        convertView = inflater.inflate(R.layout.blogslist_item, null);

                                        TextView name = (TextView) convertView.findViewById(R.id.blogs_name);
                                        TextView content = (TextView) convertView.findViewById(R.id.blogs_content);

                                        name.setText(blogs.getName());
                                        content.setText(blogs.getContent());

                                        return convertView;
                                    }
                                };
                                mBlogList.setAdapter(arr_aAdapter);*//*

                                //SimpleAdapter实现2/3
                                *//*dataList = new ArrayList<Map<String, Object>>();
                                sim_aAdapter = new SimpleAdapter(HomepageFragment.this.getContext(),getData(), android.R.layout.simple_list_item_2,
                                        new String[]{"name", "content"}, new int[]{android.R.id.text1, android.R.id.text2});
                                mBlogList.setAdapter(sim_aAdapter);*//*

                                //BaseAdapter自定义适配器实现
//                                mBlogList.setAdapter(new BlogsBaseAdapter(HomepageFragment.this.getContext(), listTest));
                                mBlogsBaseAdapter = new BlogsBaseAdapter(HomepageFragment.this.getContext(), contentSave.content);
                                mBlogsBaseAdapter.refresh(contentSave.content);//自定义方法刷新数据
//                                mBlogsBaseAdapter.notifyDataSetChanged();//刷新可见试图列表
                                mBlogList.setAdapter(mBlogsBaseAdapter);
//
                                String jsonString = "{\"name\":\"怪盗kidou\",\"content\":24}";
                                Blogs blogs = gson.fromJson(jsonString, Blogs.class);
                                Log.d("TAG", blogs.getName());
                                Log.d("TAG", response.toString());
                            }
                            //SimpleAdapter实现3/3
                            *//*private List<Map<String, Object>> getData(){
                                for (int i = 0; i < contentSave.content.size(); i++){
                                    Map<String, Object>map = new HashMap<String, Object>();
                                    map.put("name", contentSave.content.get(i).getName());
                                    map.put("content", contentSave.content.get(i).getContent());
                                    dataList.add(map);
                                }
                                return dataList;
                            }*//*
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
                Volley.newRequestQueue(HomepageFragment.this.getContext()).add(jsonObjectRequest);*/
            }
        });

        return v;

    }

}
